package com.ychug.gateway.cms;

import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.business.consts.FrontConst;
import com.ychug.framework.business.dto.CmsNewsDTO;
import com.ychug.framework.core.advice.AdDirective;
import com.ychug.framework.core.advice.NewsDataListDirective;
import com.ychug.framework.util.TempleteUtile;
import com.ychug.framework.web.base.BaseController;
import com.ychug.service.cms.entity.CmsCategoryEntity;
import com.ychug.service.cms.entity.CmsNewsDataEntity;
import com.ychug.service.cms.service.ICmsCategoryService;
import com.ychug.service.cms.service.ICmsNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cms/genStatic")
public class CmsGenStaticController extends BaseController {

    @Autowired
    private ICmsNewsService cmsNewsService;

    @Autowired
    private ICmsCategoryService cmsCategoryService;

    @Autowired
    private AdDirective adDirective;

    @Autowired
    private NewsDataListDirective newsDataListDirective;


    @RequestMapping("/news")
    @ResponseBody
    public ResultAjaxHash genStaticNews(ModelAndView modelAndView, String catId, String contentId){
       CmsCategoryEntity cmsCategoryEntity = cmsCategoryService.selectCmsCategoryById(Integer.parseInt(catId));
        try {
            String newsTemplate = cmsCategoryEntity.getNewsPath();
//            String rootPath = FrontConst.getTemplateGlobal_Pc_Path();
            String rootOutPath = FrontConst.getTemplateGlobal_Pc_Out_Path();

            TempleteUtile templeteUtile = new TempleteUtile();
            Map data = new HashMap();
            data.put("ad",adDirective);
            data.put("news_list",newsDataListDirective);

            CmsNewsDataEntity cmsNewsEntity = new CmsNewsDataEntity();
            cmsNewsEntity.setTitle("333333333333333333333333333333333");
            cmsNewsEntity.setSourceUrl("http://localhost:8082/");
            data.put("newss",cmsNewsEntity);
            File outFile = new File(rootOutPath+newsTemplate+contentId+".html");
            templeteUtile.process("index222.ftl", data, outFile);
            System.out.println("恭喜，生成成功~~");

        }catch (Exception e){
            e.printStackTrace();
            return toAjax(0);
        }

        return toAjax(1);
    }

}
